using System;
using System.Collections;
using System.Collections.Generic;
using QFramework;
using Sirenix.OdinInspector;
using Sirenix.Serialization;
using UnityEngine;

namespace YDExtendTool.Scripts
{
    [BoxGroup]
    [HideReferenceObjectPicker]
    public abstract class TreeNodeBase
    {
        [FoldoutGroup("@NodeName"), LabelText("GUID"),ReadOnly]
        public readonly string Guid = System.Guid.NewGuid().ToString();
        [FoldoutGroup("@NodeName"), LabelText("位置")]
        public Vector2 Position;
        [FoldoutGroup("@NodeName"), LabelText("名称")]
        public virtual string NodeName
        {
            get
            {
                if (Name.IsNullOrEmpty()) 
                    return GetType().ToString();
                return Name;
            }
            set => Name = value;
        }

        public virtual string Description => NodeName;

        [FoldoutGroup("@NodeName"), LabelText("名称")] public string Name;

        public virtual void Init() { }

        /// <summary>
        /// 重写该属性，用于在编辑器中，阻止不相关类型节点的连接。
        /// </summary>
        public virtual Type TreeNodeBaseType => typeof(TreeNodeBase);
    }

    public abstract class TreeNodeMultipleNextBase : TreeNodeBase
    {
        [ShowInInspector,FoldoutGroup("@NodeName"), LabelText("下一个节点列表")]
        public List<TreeNodeBase> _nodes = null;
        public List<TreeNodeBase> ChildNodes {
            get { return _nodes ??= (_nodes = new List<TreeNodeBase>()); }
        }
        


        public virtual void OnConnected(TreeNodeBase node)
        {
        }
    }

    public abstract class TreeNodeDataBase : SerializedScriptableObject,IEnumerable<TreeNodeBase>
    {
        [OdinSerialize] public TreeNodeBase RootNode;
        // 树实现的功能的描述
        public string treeDescription;
        protected abstract string ResourcePath();
        
#if UNITY_EDITOR
        [Button]
        public void OpenTreeDataCreator()
        {
            TreeDataSetting.Instance.EditTreeDataPath = ResourcePath();
            UnityEditor.EditorApplication.ExecuteMenuItem("Window/UI Toolkit/TreeDataCreator");
        }
#endif
        [Obsolete]
        public IEnumerator<TreeNodeBase> GetEnumerator()
        {
            return new TreeNodeDataEnumerator(RootNode);
        }

        [Obsolete]
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
    
    public class TreeNodeDataEnumerator:IEnumerator<TreeNodeBase>
    {
        private TreeNodeBase _root;
        private List<TreeNodeBase> _children = new();
        private int _index;
        public TreeNodeDataEnumerator(TreeNodeBase root)
        {
            _root = root;
            Reset();
        }

        public bool MoveNext()
        {
            if (_index >= _children.Count-1)
            {
                //将当前列表中的节点的子节点加入列表
                for (int i = 0; i <= _index; i++)
                {
                    if (_children[i] is TreeNodeMultipleNextBase node)
                    {
                        foreach (var value in node.ChildNodes)
                        {
                            _children.Add(value);
                        }
                    }
                }
                // 删除原本节点
                for (int i = _index; i >=0 ; i--)
                {
                    _children.RemoveAt(i);
                }
                _index = -1;
            }
            if (_children.Count == 0)
            {
                return false;
            }
            _index++;
            return true;
        }

        public void Reset()
        {
            _children.Clear();
            _children.Add(_root);
            _index = -1;
        }

        public TreeNodeBase Current => (TreeNodeBase)_children[_index];

        object IEnumerator.Current => Current;

        public void Dispose()
        {
            return;
        }
    }
}
